#include "xx_tool.h"
#include <Adafruit_NeoPixel.h>
#define PIN 32       // 数据引脚连接到8266的GPIO2
#define NUM_LEDS 60   // 灯带上的LED数量
#define BRIGHTNESS 50 // 亮度（0-255）

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

LightMode currentMode = OFF;

// 颜色生成函数
uint32_t Wheel(byte WheelPos)
{

    WheelPos = 255 - WheelPos;
    if (WheelPos < 85)
    {
        return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
    }
    if (WheelPos < 170)
    {
        WheelPos -= 85;
        return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
    }
    WheelPos -= 170;
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

// 关闭模式
void offMode()
{
    for (int i = 0; i < NUM_LEDS; i++)
    {
        strip.setPixelColor(i, strip.Color(0, 0, 0)); // 关闭所有LED
    }
    strip.show();
}

// 白光模式
void whiteMode()
{
    for (int i = 0; i < NUM_LEDS; i++)
    {
        strip.setPixelColor(i, strip.Color(255, 255, 255)); // 白色
    }
    strip.show();
}

// 暖光模式
void warmWhiteMode()
{
    for (int i = 0; i < NUM_LEDS; i++)
    {
        strip.setPixelColor(i, strip.Color(255, 193, 7)); // 暖白色
    }
    strip.show();
}

// 冷蓝模式
void coldBlueMode()
{
    for (int i = 0; i < NUM_LEDS; i++)
    {
        strip.setPixelColor(i, strip.Color(0, 191, 255)); // 冷蓝色
    }
    strip.show();
}

// 粉色模式
void pinkMode()
{
    for (int i = 0; i < NUM_LEDS; i++)
    {
        strip.setPixelColor(i, strip.Color(255, 20, 147)); // 粉色
    }
    strip.show();
}

// 紫色模式
void purpleMode()
{
    for (int i = 0; i < NUM_LEDS; i++)
    {
        strip.setPixelColor(i, strip.Color(128, 0, 128)); // 紫色
    }
    strip.show();
}

// 流光模式
void rainbowFlowMode(uint8_t wait)
{
    uint16_t i, j;

    for (j = 0; j < 256 * 5; j++)
    { // 5 cycles of all colors on wheel
        for (i = 0; i < strip.numPixels(); i++)
        {
            strip.setPixelColor(i, Wheel((i + j) & 255));
        }
        strip.show();
        delay(wait);
    }
}

// 设置当前模式
void xx_let_setLightMode(LightMode mode)
{
    currentMode = mode;
    switch (mode)
    {
    case OFF:
        offMode();
        break;
    case WHITE:
        whiteMode();
        break;
    case WARM_WHITE:
        warmWhiteMode();
        break;
    case COLD_BLUE:
        coldBlueMode();
        break;
    case PINK:
        pinkMode();
        break;
    case PURPLE:
        purpleMode();
        break;
    case RAINBOW_FLOW:
        rainbowFlowMode(20);
        break;
    }
}

// 设置颜色
void xx_let_setColor(int R, int G, int B)
{
    for (int i = 0; i < NUM_LEDS; i++)
    {
        strip.setPixelColor(i, strip.Color(R, G, B)); // 自定义颜色
    }
    strip.show();
}

// 函数：根据输入的段数，将灯带分成若干段并为每段设置不同的颜色
void xx_let_segmentColors(int numSegments)
{
    if (numSegments <= 0 || numSegments > NUM_LEDS)
    {
        return; // 输入无效时返回
    }

    int ledsPerSegment = NUM_LEDS / numSegments; // 每段的LED数量
    int remainder = NUM_LEDS % numSegments;      // 处理剩余的LED

    for (int i = 0; i < numSegments; i++)
    {
        uint8_t r = random(256); // 随机红色分量
        uint8_t g = random(256); // 随机绿色分量
        uint8_t b = random(256); // 随机蓝色分量

        int startLed = i * ledsPerSegment;
        int endLed = startLed + ledsPerSegment;

        // 如果是最后一段，处理剩余的LED
        if (i == numSegments - 1)
        {
            endLed += remainder;
        }

        for (int j = startLed; j < endLed; j++)
        {
            strip.setPixelColor(j, strip.Color(r, g, b));
        }
    }

    strip.show(); // 刷新显示
}

// 初始化
void xx_led_setup()
{
    strip.setBrightness(BRIGHTNESS);
    strip.show(); // 初始化时将所有LED关闭
    xx_let_setLightMode(currentMode);
}